package com.seven.first.suite.util;


import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String SIMPLE_FORMAT = "yyyy-MM-dd";

    public static String format(Date date) {
        if (null != date) {
            SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
            return format(date, sdf);
        } else {
            return null;
        }
    }

    public static String format(Date date, DateFormat format) {
        return null != format && null != date ? format.format(date) : null;
    }

    public static Date parse(String date) {
        if (StringUtils.isNotBlank(date)) {
            SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
            try {
                return sdf.parse(date);
            } catch (ParseException e) {
            }
        }
        return null;
    }

    public static Date getBeginDay() {
        SimpleDateFormat format = new SimpleDateFormat(SIMPLE_FORMAT);
        String day = format(new Date(),format);
        StringBuilder sb = new StringBuilder();
        sb.append(day).append(" 00:00:00");
        return parse(sb.toString());
    }

    public static Date getendDay() {
        SimpleDateFormat format = new SimpleDateFormat(SIMPLE_FORMAT);
        String day = format(new Date(),format);
        StringBuilder sb = new StringBuilder();
        sb.append(day).append(" 23:59:59");
        return parse(sb.toString());
    }

    public static void main(String[] args) {
        System.out.println(new Date());
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 1);
        System.out.println(c.getTime());
    }
}
